require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UICharactorAttributeDetails = {};
HelperFunc.SetUIEnv(UICharactorAttributeDetails)
local this = UICharactorAttributeDetails;
---@type FairyGUI.GComponent
this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.attributeShowConfig = nil;

this.hasInit = false;

this.currentHeroData = {};
this.baseAttr = {}

this.btnClose = nil;
---@type FairyGUI.GList
this.gridItemParent = nil;
this.items = {};
this.indexs = {};
this.isPreview = nil
this.starAdd = 0
this.lvAdd = 0
this.attrInfoCtrl = nil
this.descAttrName = nil
this.descInfo = nil

function UICharactorAttributeDetails.Init(heroData,preview)--todo 应该小写 暂时先大写
    local name,package = getUIName(PanelNames.UICharactorAttributeDetails);
    if attributeShowConfig==nil then
        attributeShowConfig = G.dataTable["t_attributelibrary"];
    end
    isPreview = preview
    if isPreview then
        starAdd =  G.dataTable["t_herorankupco"]["150"]["f_RankUpCo"] / 10000
        lvAdd =  dataTable.getDataTableByStringId("t_levelupattribute",MakeTopLevelId( "350"),"f_Para") / 10000
        baseAttr = heroData.baseAttr
    end
    table.clear(currentHeroData)
    table.clear(indexs)
    for k,v in pairs(heroData["totalAttribute"]) do
        local attributeItemConfig = attributeShowConfig[tostring(k)];
        if attributeItemConfig.f_IsPanel ==1 then
            local item = {}
            item.v = v
            item.k = k
            table.insert(currentHeroData,item)
            table.insert(indexs,k);
        end
    end
    table.sort(currentHeroData,function(a,b)
        local config_a  = attributeShowConfig[tostring(a.k)];
        local config_b  = attributeShowConfig[tostring(b.k)];
        if config_a.f_Sort <= config_b.f_Sort then
            return true
        end
    end)
    table.sort(indexs,function(a,b)
        local config_a  = attributeShowConfig[tostring(a)];
        local config_b  = attributeShowConfig[tostring(b)];
        if config_a.f_Sort <= config_b.f_Sort then
            return true
        end
    end)
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI);

end
---@param obj FairyGUI.GComponent
function UICharactorAttributeDetails.onCreate(obj)
    gameObject = obj;
   -- transform = obj.transform;
    --LuaBehaviour = transform:GetComponent('LuaBehaviour');
    
    if not hasInit then
        hasInit = true;
        btnClose = obj:GetChild("CloseBtn")
        gridItemParent = obj:GetChild("Grid");
        this.attrInfoCtrl = obj:GetController("attrInfo")
        this.attrInfoCtrl.selectedIndex = 0
        this.addEvent();
        local descList = obj:GetChild("desc")
        descList:RemoveChildrenToPool()
        local gObj = descList:AddItemFromPool()
        this.descAttrName = gObj:GetChild("AttrName")
        this.descInfo = gObj:GetChild("AttrDecs")
    end

    this.refresh();
end

function UICharactorAttributeDetails.onClickAttr(context)
    local go = context.data;
    local indexId = gridItemParent:GetChildIndex(go) + 1

    this.attrInfoCtrl.selectedIndex = 1
    local name = getLanguage(string.format(Config.LangAttribute,indexs[indexId]))
    local descStr = getLanguage(string.format(Config.LangDescAttribute,indexs[indexId]))
    this.descAttrName.text = name
    this.descInfo.text = descStr
end

function UICharactorAttributeDetails.addEvent()
    btnClose.onClick:Add(this.onClickClose)
    --LuaBehaviour:AddClick(btnClose.gameObject, this.onClickClose);
    gridItemParent.onClickItem:Add(this.onClickAttr);

end

function UICharactorAttributeDetails.removeEvent()
    btnClose.onClick:Remove(this.onClickClose)
   -- LuaBehaviour:RemoveClick(btnClose.gameObject);
    this.gridItemParent.onClickItem:Remove(this.onClickAttr)
end

function UICharactorAttributeDetails.LocalListenEvent()

end

function UICharactorAttributeDetails.LocalCloseEvent()

end

function UICharactorAttributeDetails.OnDestroy()
    this.removeEvent();
end

function UICharactorAttributeDetails.OnEnable()
    this.LocalListenEvent();
end

function UICharactorAttributeDetails.OnDisable()
    this.LocalCloseEvent();
end

function UICharactorAttributeDetails.GridItemRender(index,item)
    index = index + 1
    local attr = currentHeroData[index].v
    if isPreview then
        local attrType = baseAttr[indexs[index]][1]
        if EnumConst.FighterAttributeEnum.ATK == attrType or EnumConst.FighterAttributeEnum.HP == attrType or EnumConst.FighterAttributeEnum.DEF == attrType then
            attr =  Mathf.Round(starAdd * Mathf.Round(lvAdd * baseAttr[indexs[index]][2]))
        else
            attr = baseAttr[indexs[index]][2]
        end
    end
    local attributeItemConfig = attributeShowConfig[tostring(indexs[index])];
    if attributeItemConfig.f_IsPanel ==1 then
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.UICharactorAttributeDetailsItem);
        if ctrlItem ~=nil and item~=nil then
            local data = {
                name = getLanguage(string.format(Config.LangAttribute,indexs[index])),
                value = attr,
                type = attributeItemConfig.f_AttributeType,
                icon = "attr"..indexs[index]
            };
            ctrlItem.initData(item,data,LuaBehaviour);
        end
    end
end
function UICharactorAttributeDetails.refresh()
    gridItemParent.itemRenderer = this.GridItemRender
    gridItemParent.numItems = #currentHeroData
end

function UICharactorAttributeDetails.onClickClose()
    closeUI(PanelNames.UICharactorAttributeDetails);
end



return UICharactorAttributeDetails;
